//
//  GDGuideView.m
//  GD新特性界面
//
//  Created by 胡卓 on 2017/3/22.
//  Copyright © 2017年 胡卓. All rights reserved.
//

#import "GDGuideView.h"
#import "Masonry.h"

@interface GDGuideView () <UIScrollViewDelegate>

@property(nonatomic,weak) UIScrollView *scrollView;
@property(nonatomic,weak) UIPageControl *pageControl;

@end


@implementation GDGuideView


// 只要scrollView里面的内容在滚动就会调用此方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    // 滚动出新的一页只要出来一半多一点点就显示下一页
    NSInteger page = scrollView.contentOffset.x / scrollView.bounds.size.width + 0.499999;
    
    _pageControl.currentPage = page;
    
    // 如果是显示出来空白页时让分页指示器隐藏,反之应该显示
    _pageControl.hidden = (_imageNames.count == page);
    
    scrollView.tag = page;
}

#pragma mark - 完全停止滚动时会调用一次此方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // 如果停下来之后显示的是空白页,那就把新特性界面移除
    if (scrollView.tag == _imageNames.count) {
        
        [self removeFromSuperview];
    }
    
}

//重写控件的初始化方法initWithFrame
-(instancetype) initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        //添加scrollView
        [self makeScrollView];
        
        // 创建分页指示器
        UIPageControl *pageControl = [[UIPageControl alloc] init];
        _pageControl = pageControl;
        // 一定不要添加到scrollView上了
        [self addSubview:pageControl];
        // 当前显示第几页
        pageControl.currentPage = 0;
        // 设置其它页点的颜色
        pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
        // 设置当前页点的颜色
        pageControl.currentPageIndicatorTintColor = [UIColor redColor];
        
        [pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.mas_bottom).offset(-20);
            make.centerX.equalTo(self.mas_centerX).offset(0);
        }];
        
        // 让pageControl不能点击
        pageControl.enabled = NO;
        
    }
    return self;
}


#pragma  mark - 添加及设置scrollView
-(void) makeScrollView {
    
    //MARK: 1.创建scrollView
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    //MARK: 3.给属性赋值
    _scrollView = scrollView;
    //MARK: 2.添加scrollView到guideView上
    [self addSubview:scrollView];
    //MARK: 4.添加约束
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).offset(0);
    }];
    
    //MARK: 5.设置分页
    _scrollView.pagingEnabled = YES;
    //MARK: 6.禁用弹簧效果
    _scrollView.bounces = NO;
    //MARK: 7.隐藏滚动条
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    
    //MARK: 8.给scrollView设置代理
    _scrollView.delegate = self;
    
}


#pragma  mark --重写setter方法，拿到数据后给scrollView添加子控件
-(void) setImageNames:(NSArray *)imageNames {
    
    _imageNames = imageNames;
    //定义scrollView的宽
    CGFloat scrollViewW = self.bounds.size.width;
    // 定义scrollView的高
    CGFloat scrollViewH = self.bounds.size.height;
    
    // for循环创建及添加imageView
    for (NSInteger i = 0; i < imageNames.count; i++) {
        
        // 1.创建imageView
        UIImageView *imageView = [[UIImageView alloc] init];
        
       
        // 2.根据传过来的图片名称去获取图片文件路径
        NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageNames[i] ofType:@"jpg"];
        // 3.通过路径去加载
        UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
        
        // 4.给imageView设置图片
        imageView.image = image;
    
        // 5.设置imageView的frame 图片是水平一个接一个的排列,所以imageView的X是不一样的
        imageView.frame = CGRectMake(scrollViewW * i, 0, scrollViewW, scrollViewH);
        imageView.contentMode = UIViewContentModeScaleToFill;
        
        
        // 6.把imageView添加到scrollView上
        [_scrollView addSubview:imageView];
        
        
        //给最后一张图片添加一个button
        if (i == imageNames.count -1) {
            
            [self makeLastBtn:imageView];
        }
        // imageView默认是不能进行点击的,所以它里面的子控件也不能点击,如果想让它可以点击应该让它可以和用户交互
        imageView.userInteractionEnabled = YES;
    }
    
    // 7.设置scrollView的滚动范围
    _scrollView.contentSize = CGSizeMake((imageNames.count + 1) * scrollViewW, 0);
    
    // 8.设置分页指示器的点的总个数
    _pageControl.numberOfPages = imageNames.count;
}

-(void) makeLastBtn:(UIImageView *) imageView {
    
    //创建button
    UIButton *btn = [[UIButton alloc]init];
    //给按钮设置标题和颜色
    [btn setTitle:@"Go" forState:UIControlStateNormal];
    [btn setTintColor:[UIColor purpleColor]];
    btn.titleLabel.font = [UIFont systemFontOfSize:30];
    
    
    [imageView addSubview:btn];
    // 让按钮根据内部自适应大小
    [btn sizeToFit];
    
    // 设置位置约束
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_offset(CGSizeMake(80, 50));
        make.right.equalTo(imageView.mas_right).offset(-20);
        make.bottom.equalTo(imageView.mas_bottom).offset(-50);
    }];
    
    // 给按钮添加监听
    [btn addTarget:self action:@selector(removeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
}

#pragma mark -- 按钮监听方法
-(void) removeBtnClick:(UIButton *) btn {
    // 1.先把当前点击的按钮隐藏
    btn.hidden = YES;
    // 2.把当前被点击按钮的父控件放大,
    [UIView animateWithDuration:1.0 animations:^{
        // 让imageView慢慢放大
        btn.superview.transform = CGAffineTransformMakeScale(1.5, 1.5);
        // 慢慢透明
        btn.superview.alpha = 0;
    } completion:^(BOOL finished) {
        // 直接把新特性界面全部移除
        // 3.进入主界面"把新特性界面删除"
        [self removeFromSuperview];
    }];
    
}
@end
